import { _decorator, Component, Node, Vec2, Vec3 } from 'cc';
import { EnemyState } from './EnemyState';
import { EnemyStateMachine } from './EnemyStateMachine';
import { Enemy } from './Enemy';
const { ccclass, property } = _decorator;

@ccclass('EnemyMoveState')
export class EnemyMoveState extends EnemyState {

    constructor(_enemy: Enemy, _stateMachine: EnemyStateMachine) {
        super(_enemy, _stateMachine);
    }
    override enter(): void {
        super.enter();
        this.enemy.playAnimation("yidong", true);

    }
    override update(dt: number): void {
        super.update(dt);
        if (this.enemy.isDead) return;
        if (this.enemy.isPlayerInAttackRange()) {
            this.stateMachine.changeState(this.enemy.battleState);
            return;
        }
        this.moveToPlayer(dt);
    }
    override exit(): void {
        super.exit();

    }
    moveToPlayer(dt: number) {
        const enemyPos = this.enemy.node.position;
        const playerPos = this.enemy.player.position;

        const direction = new Vec3();
        Vec3.subtract(direction, playerPos, enemyPos);
        direction.normalize();

        const moveVector = new Vec2(direction.x * this.enemy.moveSpeed, direction.y * this.enemy.moveSpeed);

        this.rb.linearVelocity = moveVector;


    }
}


